#include <stdio.h>
#include <mpx_supt.h>
#include "mrj_supt.h"

date_rec* dateP = new date_rec;

void help()
{
	printf("\n\n This is a comprehensive list of commands and how they're used.\n COMMAND: \t\t\t\t DESCRIPTION:\n");
	printf("\t dispD \t\t\t Displays current date.\n");
	printf("\t dispMPX \t\t Displays contents of directory specified. \n\t\t\t\t Displays . (current directory) by default.\n\t\t\t\t ex dispD /path/dir \n");
	printf("\t help \t\t\t Displays this help menu.\n");
	printf("\t setD \t\t\t Command used to set date. ex setD mm/dd/yyyy \n");
	printf("\t vrsn \t\t\t Displays version of MPX. \n");
	printf("\t exit \t\t\t Exits program on confirmation. ex y or n\n");

}

void version()
{
	printf("\n%.1f\n", VERSION);
}

void displayDate()
{
	sys_get_date(dateP);
	printf("\n%d/", dateP->month);
	printf("%d/", dateP->day);
	printf("%d\n\n", dateP->year);
}

void changeDate(int m, int d, int y)
{
	int valid = FALSE;           //date validation parameter

	if(y<0) {valid=TRUE;}    //Year is a negative number

	if(m>12 || m<1) {valid=TRUE;} //Month must be 1-12

	if (y%400 == 0 || (y%4 == 0 && y%100 != 0))   //Leap year
	{
		if(m==2 && d>29) {valid=TRUE;}  //29 days in feb during leap year
	}
	
	if (!(y%400 == 0 || (y%4 == 0 && y%100 != 0)))   //Not Leap year
	{
		if(m==2 && d>28) {valid=TRUE;}  //28 days in feb
	}

	else if((m==4 || m==6 || m==9 || m==11) && d>30) {valid=TRUE;}//30 days

	else if((m==1 || m==3 || m==5 || m==7 || m==8
	|| m==10 || m==12) && d>31) {valid=TRUE;}        //31 day months



	if(valid)           //Valid date, execute sys_set_date()
	{
		dateP->month = m;
		dateP->day = d;
		dateP->year = y;
		sys_set_date(dateP);
	}

	else if(valid==FALSE)    //invalid date
	{ printf("Invalid date, please try again.\n");}
}

void displayMPX(char* dir_path)
{
		//declaring
  int error_check;
  char file_name_buf[BUF_LEN];
  file_name_buf[strlen(file_name_buf)-1]='\0';
  long *file_size_p;
  printf("dir path: %s \n", dir_path);
  //open directory specified
  if(dir_path[0]!='\n'){
	//printf("%d", sys_open_dir(dir_path));
	dir_path[strlen(dir_path)-1] = '\0';
	sys_open_dir(dir_path); //look up directory to open
	}
  else{
	//printf("%d", sys_open_dir('\0'));
	sys_open_dir('\0');
	}
 
  //do block is used to grab each mpx file, display it and look for next
  error_check = sys_get_entry(file_name_buf, 20, file_size_p);
  //printf("error=%d\n", error_check);
  while (error_check != ERR_SUP_NOENTR){
	error_check = sys_get_entry(file_name_buf, 20, file_size_p);
    printf("%s \t\t %d \n", file_name_buf, file_size_p);
	}
	
  //close directory
  sys_close_dir();
}

void terminateMPX()
{
	sys_exit();
}

void commhan()
{
	char bufC[BUF_LEN];
	int buf_sizeC = BUF_LEN;
	sys_req(CLEAR, TERMINAL, bufC, &buf_sizeC);

	printf("Welcome. For a list of commands type help into\nthe command prompt. After typing a command to\nthe system you will be prompted again to enter\nthe needed information. This system is NOT case\nsensitive.\n");
			
	int exit = 0;
	while(!exit)         //run until exit command is executed
	{
		char bufWithSpaces[BUF_LEN];       //input with spaces
		int buf_size = BUF_LEN;

		printf("USER> ");          //input prompt
		sys_req(READ, TERMINAL, bufWithSpaces, &buf_size);
		bufWithSpaces[strlen(bufWithSpaces)-1]='\0';

		char buf[BUF_LEN];
		int counter = 0;
		
		for(int i=0; i<buf_size; i++)                //Gets rid of spaces in input
		{
			if(bufWithSpaces[i] != ' ')
			{
				buf[counter] = bufWithSpaces[i];
				counter++;
			}
		}

		
		if(((buf[0] == 'h') || (buf[0] == 'H')) && ((buf[1] == 'e') || (buf[1] == 'E')) //help
		&& ((buf[2] == 'l') || (buf[2] == 'L')) && ((buf[3] == 'p') || (buf[3] == 'P'))
		&& (buf[4] == '\0'))
		{
			help();
		}
		
		else if(((buf[0] == 'v') || (buf[0] == 'V')) && ((buf[1] == 'r') || (buf[1] == 'R')) //vrsn
		&& ((buf[2] == 's') || (buf[2] == 'S')) && ((buf[3] == 'n') || (buf[3] == 'N'))
		&& (buf[4] == '\0'))
		{
			version();
		}
		
		else if(((buf[0] == 'd') || (buf[0] == 'D')) && ((buf[1] == 'i') || (buf[1] == 'I')) //dispD
		&& ((buf[2] == 's') || (buf[2] == 'S')) && ((buf[3] == 'p') || (buf[3] == 'P'))
		&& ((buf[4] == 'd') || (buf[4] == 'D')) && (buf[5] == '\0'))
		{
			displayDate();
		}
		
		else if(((buf[0] == 's') || (buf[0] == 'S')) && ((buf[1] == 'e') || (buf[1] == 'E')) //setD
		&& ((buf[2] == 't') || (buf[2] == 'T')) && ((buf[3] == 'd') || (buf[3] == 'D'))
		&& (buf[4] == '\0'))
		{
			printf("	Enter new date(mm/dd/yyyy) -> ");
			buf_size = BUF_LEN;
			bufWithSpaces[0]='\0';
			buf[0]='\0';
			
			sys_req(READ, TERMINAL, bufWithSpaces, &buf_size);
			bufWithSpaces[strlen(bufWithSpaces)-1]='\0';         

			counter = 0;
			for(int i=0; i<buf_size; i++)                //Gets rid of spaces in input
			{
				if(bufWithSpaces[i] != ' ')
				{
					buf[counter] = bufWithSpaces[i];
					counter++;
				}
			}
			
			if((strlen(buf)-1)!=9){printf("		Date was not in the(mm/dd/yyyy) format\n");}//invalid			
			
			else               //valid input
			{
				int month = ((int(buf[0])-48)*10)+((int(buf[1])-48));         
				int day = ((int(buf[3])-48)*10)+((int(buf[4])-48));
				int year = ((int(buf[6])-48)*1000)+((int(buf[7])-48)*100)
					+((int(buf[8])-48)*10)+((int(buf[9])-48));
				changeDate(month, day, year);               
			}												
		}        	                                       
		
		else if(((buf[0] == 'd') || (buf[0] == 'D')) && ((buf[1] == 'i') || (buf[1] == 'I')) //dispMPX
		&& ((buf[2] == 's') || (buf[2] == 'S')) && ((buf[3] == 'p') || (buf[3] == 'P'))
		&& ((buf[4] == 'm') || (buf[4] == 'M'))  && ((buf[5] == 'p') || (buf[5] == 'P'))
		&& ((buf[6] == 'x') || (buf[6] == 'X')) && (buf[7] == '\0'))
		{
			printf("	Enter the directory -> ");
			buf_size = BUF_LEN;
			sys_req(READ, TERMINAL, buf, &buf_size);
			//buf[strlen(buf)-1]='\0';
			if((strlen(buf)) == 0)
			{
				char empty[10];
				empty[0]=0;
				displayMPX(empty);
			}

			else {displayMPX(buf);}
		}

		else if(((buf[0] == 'e') || (buf[0] == 'E')) && ((buf[1] == 'x') || (buf[1] == 'X')) //exit
		&& ((buf[2] == 'i') || (buf[2] == 'I')) && ((buf[3] == 't') || (buf[3] == 'T'))
		&& (buf[4] == '\0'))
		{
			printf("Are you sure you want to exit?\n");   //exit confirmation
			printf("	(Y/N) -> ");
			buf_size=BUF_LEN;
			sys_req(READ, TERMINAL, bufWithSpaces, &buf_size);
			
			counter=0;
			for(int i=0; i<buf_size; i++)                //Gets rid of spaces in input
			{
				if(bufWithSpaces[i] != ' ')
				{
					buf[counter] = bufWithSpaces[i];
					counter++;
				}
			}
			
			if((buf[0] == 'y') || (buf[0] == 'Y')) { exit=1;}   //yes exit
			else if((buf[0] == 'n') || (buf[0] == 'N')) {exit=0;} //no don't exit
			else {printf("Invalid input, enter either a 'Y' or 'N'\n");} //wrong answer
		}

		else {printf("Unrecognized command, type help for assistance\n");}
	}
	
}
